﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
// Copyright (C) 2021 kingerrobot
// 版权所有。 
//
// 文件名：BWcsAsrsCrnOutPut
// 文件功能描述：
//
// 创建者：名字 (zzl)
// 时间：2021/12/23 13:43:42
//
// 修改人：
// 时间：
// 修改说明：
// 版本：V1.0.0
//----------------------------------------------------------------*/
#endregion
namespace KingerRobot.MexicoSailunMDC.SpiService;

internal class BWcsAsrsCrnOutPut : IWcsAsrsCrnOutPut
{

    private readonly static IRcsLocation _RcsLocation = new BRcsLocation();
    private readonly static IWcsAsrsCrnOutPut _WcsAsrsCrnOutPut = new BWcsAsrsCrnOutPut();
    private readonly static ITask _Task = new BTask();
    private readonly static ITaskCmd _TaskCmd = new BTaskCmd();

    public List<POWcsAsrsCrnOutput> GetWcsAsrsCrnOutputs()
    {
        var db = Db.Default;
        var result = db.WcsAsrsCrnOutputs
            .ToList();
        return result;
    }
    public List<POWcsAsrsCrnOutput> GetWcsAsrsCrnOutputsByCrnId(long crnId)
    {
        var db = Db.Default;
        var result = db.WcsAsrsCrnOutputs.Where(t => t.CrnId == crnId && t.UseFlag == 1)//&& t.AreaId != 10020201
            .ToList();
        return result;
    }

    public List<POWcsAsrsCrnOutput> GetWcsAsrsCrnAbnormalOutputsByCrnId(long crnId)
    {
        var db = Db.Default;
        var result = db.WcsAsrsCrnOutputs.Where(t => t.CrnId == crnId && t.UseFlag == 1 && t.AreaId != 10020201 )
            .ToList();
        return result;
    }


    public POWcsAsrsCrnOutput GetAsrsCrnOutputByOutPutLocId(long outPutLocId)
    {
        var db = Db.Default;
        var result = db.WcsAsrsCrnOutputs
            .Where(t => t.OutputLoc1 == outPutLocId || t.OutputLoc2 == outPutLocId)
            .FirstOrDefault();
        return result;
    }

    public List<CrnOutLoc> GetNoSkuLoc(List<long?> crnIds)
    {
        var outputs = GetWcsAsrsCrnOutputByCrnIds(crnIds);
        if (!outputs.Any())
        {
            return null;
        }
        var loc1 = outputs.Select(t => t.OutputLoc1).ToList();
        var loc2 = outputs.Select(t => t.OutputLoc2).ToList();
        var ids = loc1.Union(loc2).ToList();

        var locations = _RcsLocation.GetLocationsByLocIds(ids);
        var locs = new List<CrnOutLoc>();

        outputs.ForEach(item =>
        {
            var location1 = locations.Where(t => t.Id == item.OutputLoc1 && t.LocHasSku == 0).FirstOrDefault();
            var location2 = locations.Where(t => t.Id == item.OutputLoc2 && t.LocHasSku == 0).FirstOrDefault();

            var noSkuLoc = new CrnOutLoc();
            var exLocInfos = new List<ExLocInfo>();
            if (location1 != null)
            {
                noSkuLoc.CrnId = item.CrnId;
                noSkuLoc.AreaId = item.AreaId;
                exLocInfos.Add(new()
                {
                    LocId = item.OutputLoc1,
                    HasGood = true,
                    Index = 1,
                });

                if (location2 != null)
                {
                    exLocInfos.Add(new()
                    {
                        LocId = item.OutputLoc2,
                        HasGood = true,
                        Index = 2,
                    });
                }

                locs.Add(noSkuLoc);
            }

            noSkuLoc.ExLocInfos = exLocInfos;
        });

        return locs;
    }


    public List<CrnOutLoc> GetAllOutLocV2(List<long?> crnIds)
    {
        var outputs = GetWcsAsrsCrnOutputByCrnIds(crnIds);
        if (!outputs.Any())
        {
            return new List<CrnOutLoc>();
        }

        // 出库限制任务数量
        // var wcsAsrsCrnOutputs = GetWcsAsrsCrnOutputs();

        // 任务出库数量
        //var outputGroup = _Task.GetTargetLocTaskGroup();

        // 任务出库数量 改为用指令 (可执行任务)
        var outputGroup = _TaskCmd.GetExecutableTargetLocTaskGroup();


        var locs = new List<CrnOutLoc>();
        foreach (var item in outputs)
        {
            var loc1 = item.OutputLoc1;
            var loc2 = item.OutputLoc2;

            // 任务总数
            var loc1_count = outputGroup.Where(t => t.LocId == loc1).FirstOrDefault()?.Count ?? 0;
            var loc2_count = outputGroup.Where(t => t.LocId == loc2).FirstOrDefault()?.Count ?? 0;

            // var number = wcsAsrsCrnOutputs.Where(t => t.OutputLoc1 == loc1 || t.OutputLoc2 == loc2).FirstOrDefault()?.TaskCount ?? 999;
            var number = item.TaskCount ?? 0;
            if (number - (loc1_count + loc2_count) <= 0)
            {
                continue;
            }

            locs.Add(new CrnOutLoc
            {
                CrnId = item.CrnId,
                AreaId = item.AreaId,
                TaskCount = number,
                NowCount = loc1_count + loc2_count,
                ExLocInfos = new List<ExLocInfo>{
                    new()
                    {
                        LocId = loc1,
                        Index = 1,
                    },
                    new()
                    {
                       LocId = loc2,
                       Index = 2,
                    }
                }
            });
        }
        //WriteLog($"可用出库放货位1{locs.ToJson()}", true);
        return locs;
    }
    public List<CrnOutLoc> GetAllOutLoc(List<long?> crnIds)
    {

        return GetAllOutLocV2(crnIds);
    }

    public List<POWcsAsrsCrnOutput> GetWcsAsrsCrnOutputALLByEquipId(long crnId)
    {
        var db = Db.Default;
        //var result = this.GetWcsAsrsCrnOutputs().Where(t => t.UseFlag == 1 && t.CrnId == crnId).ToList();
        var result = this.GetWcsAsrsCrnOutputsByCrnId(crnId);
        return result;
    }
    public List<POWcsAsrsCrnOutput> GetWcsAsrsCrnOutputByEquipId(long crnId)
    {
        var db = Db.Default;
        //var result = this.GetWcsAsrsCrnOutputs().Where(t => t.UseFlag == 1 && t.CrnId == crnId).ToList();
        var result = this.GetWcsAsrsCrnOutputsByCrnId(crnId);
        return result;
    }

    //GetWcsAsrsCrnAbnormalOutputsByCrnId

    private List<POWcsAsrsCrnOutput> GetWcsAsrsCrnOutputByCrnIds(List<long?> crnIds)
    {
        var db = Db.Default;
        var result = db.WcsAsrsCrnOutputs.Where(t => t.UseFlag == 1 && crnIds.Contains(t.CrnId)).ToList();
        return result;
    }

}